package com.auto.survey.activity.config

import android.content.Context
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.auto.survey.R
import com.auto.survey.activity.config.entity.PointConfig
import com.auto.survey.base.ICreatView
import com.auto.survey.util.Helper
import com.auto.survey.widget.CentreToast
import com.google.gson.Gson
import kotlinx.coroutines.launch


/**
 * 测点配置
 */
class ConfigSurveyPointView(pointConfigs:MutableList<PointConfig>, val viewModel: ConfigViewModle, val activity: AppCompatActivity) :
    ICreatView {
    companion object {
        private val TAG:String = ConfigSurveyPointView::class.java.simpleName
    }

    private var mPointConfigs = pointConfigs
    private val mActivity = activity
    private lateinit var mView: View
    private lateinit var recyclerView:RecyclerView
    override fun creatView(context: Context, parent: ViewGroup) {
        mView = LayoutInflater.from(context).inflate(R.layout.view_serveypoint_config, parent, false)
        recyclerView = mView.findViewById(R.id.rv_add_point)
        recyclerView.layoutManager = LinearLayoutManager(context)


        // 创建适配器
        val adapter = SurveyPointAdapter(mActivity)
        recyclerView.adapter = adapter

        // 配置 ItemTouchHelper 处理侧滑和拖拽
        val callback = SwipeMenuCallback(adapter)
        val itemTouchHelper = ItemTouchHelper(callback)
        itemTouchHelper.attachToRecyclerView(recyclerView)

        // 设置适配器的 recyclerView 和 itemTouchHelper 引用
        adapter.itemTouchHelper = itemTouchHelper


        mView.findViewById<TextView>(R.id.tv_add_point).setOnClickListener(Helper.debounce {
            val time = System.currentTimeMillis()
            val point = PointConfig(configId = 0, point_x_data= 0.0, point_y_data=0.0, point_z_data=0.0, point_mirr_h=0.0,point_mirr_num=time, point_name="P${time}")
            mPointConfigs.add(point)
            adapter.setItems(mPointConfigs)
            recyclerView.smoothScrollToPosition(mPointConfigs.size)
        })

        mActivity.lifecycleScope.launch {
            viewModel.bean.observe(mActivity) { it->
                Log.e(TAG,"数据库查询测点配置==> : ${Gson().toJson(it?.points)}")

                it?.points?.let { it1 -> mPointConfigs.addAll(it1) }
                adapter.setItems(mPointConfigs)
            }
        }
    }



    override fun checkView(): Boolean {

        mPointConfigs.forEach { pointConfig ->
            if (TextUtils.isEmpty(pointConfig.point_name)) {
                CentreToast.instance.show("测点编号：${pointConfig.point_mirr_num} 的名称不能是空", false)
                return false
            }

            if (pointConfig.point_mirr_num <= 0) {
                CentreToast.instance.show("测点名称：${pointConfig.point_name} 的编号不能是0", false)
                return false
            }
        }
        Log.e(TAG,"checkView==> : ${Gson().toJson(mPointConfigs)}")
        viewModel.updateConfigDataByPoint(mPointConfigs)

        return true
    }

    override fun getView(): View {
        return mView
    }

    override fun getTitle(): String {
        return "测点配置"
    }
}